package server.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import commons.User;
import server.filters.RestrictedPagesToConnectedUser;
import server.forms.PasswordsForm;

/**
 * Servlet used to display some settings to the user, such as 'edit passwords'.
 * @author Romain Pellerin
 */
public class Settings extends HttpServlet {
	
	private static final long serialVersionUID = -8272602434056614704L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = RestrictedPagesToConnectedUser.getUserIfConnected(request); // USER IS NECESSARILY CONNECTED AS THIS PAGE IS FILTERED BY 'RestrictedPagesToConnectedUser'
		if (user.isAdmin())
			this.getServletContext().getRequestDispatcher("/WEB-INF/jsp_pages/settings.jsp").forward(request, response);
		else
			response.sendRedirect("console");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = RestrictedPagesToConnectedUser.getUserIfConnected(request); // USER IS NECESSARILY CONNECTED AS THIS PAGE IS FILTERED BY 'RestrictedPagesToConnectedUser'
		if (user.isAdmin()) {
			PasswordsForm pwdf = new PasswordsForm();
			pwdf.changePwds(request);
			request.setAttribute("admin_result", pwdf.getAdmin_result());
			request.setAttribute("admin_error", pwdf.getAdmin_error());
			request.setAttribute("guest_result", pwdf.getGuest_result());
			request.setAttribute("guest_error", pwdf.getGuest_error());
			this.getServletContext().getRequestDispatcher("/WEB-INF/jsp_pages/settings.jsp").forward(request, response);
		}
		else
			response.sendRedirect("console");
	}

}